package bookworm;

import java.util.Collections;
import java.util.LinkedList;

import comparators.ComparatorOfBooks;

import visitorpattern.CreatesVisitorsAndVisitsGraph;

public class SearchBestBooksBasedOnTag 
{
	private String tag;
	private LinkedList<Book> bestBooksFromTag;
	private CreatesVisitorsAndVisitsGraph createsAndVisitsGraph;
	
	public SearchBestBooksBasedOnTag(String tag)
	{
		this.tag = tag;
		createsAndVisitsGraph = new CreatesVisitorsAndVisitsGraph();
		this.bestBooksFromTag = new LinkedList<Book>();
		this.searchForBestBooksOfTag();
		this.organizeBestBooksOfTag();
	}
	
	private void searchForBestBooksOfTag()
	{
		LinkedList<Book> allBooksFromTag = 
				createsAndVisitsGraph.getBooksFromSpecificTag(tag);
		for(int i = 0; i < allBooksFromTag.size(); i++)
		{
			Book oneBook = allBooksFromTag.get(i);
			if(oneBook.getAverageRatingFromABook() > 2)
			{
				//it has an average rating better than 2 so it's a best book
				this.bestBooksFromTag.add(oneBook);
			}
		}
	}
	
	private void organizeBestBooksOfTag()
	{
		ComparatorOfBooks comparesBooks = new ComparatorOfBooks();
		Collections.sort(bestBooksFromTag, comparesBooks);
	}

	public LinkedList<Book> getBestBooksFromTag() {
		return bestBooksFromTag;
	}

	public void setBestBooksFromTag(LinkedList<Book> bestBooksFromTag) {
		this.bestBooksFromTag = bestBooksFromTag;
	}
	
	

}
